package com.kotlin.user.presenter

import com.kotlin.base.ext.execute
import com.kotlin.base.presenter.BasePresenter
import com.kotlin.base.rx.BaseSubscriber
import com.kotlin.user.presenter.view.RegisterView
import com.kotlin.user.service.impl.UserServiceImpl
import org.jetbrains.anko.AnkoLogger
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import javax.inject.Inject

/**
 * @author Mr.Wang
 * @version v0.3.6
 * @time 2018/3/7
 * @describe
 */
class RegisterPresenter @Inject constructor():BasePresenter<RegisterView>(){

        @Inject
        lateinit var userServiceImpl: UserServiceImpl

        fun register(mobile:String,pwd:String){
            /*
            业务逻辑
            * */
            if (!checkNetWork()){
                //网络不可用
                return
            }
            mView.showLoading()
            userServiceImpl.register(mobile,pwd)
                    .execute(object : BaseSubscriber<Boolean>(mView) {
                        override fun onNext(t: Boolean) {
                            mView.onRegisterResult(t)
                        }
                    })

        }
    fun login(mobile:String,pwd:String){
        /*
        业务逻辑
        * */
        val userServiceImpl = UserServiceImpl()
        userServiceImpl.register(mobile,pwd)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(object : BaseSubscriber<Boolean>(mView) {
                    override fun onNext(t: Boolean) {
                        mView.onRegisterResult(t)
                    }

                    override fun onError(e: Throwable?) {
                        AnkoLogger(e.toString())
                    }
                }
                    )


    }
}